package com.dsg.service.oil.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.dao.oil.DsgOilDoubtListMapper;
import com.dsg.entity.oil.DsgOilDoubtList;
import com.dsg.entity.system.DsgSysParameter;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.oil.IDsgOilDoubtListService;
import com.dsg.service.system.IDsgSysParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 存疑记录 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-31
 */
@Service
public class DsgOilDoubtListServiceImpl extends DsgServiceImpl<DsgOilDoubtListMapper, DsgOilDoubtList> implements IDsgOilDoubtListService {

    @Autowired
    private IDsgSysParameterService dsgSysParameterService;

    @Override
    public Page<DsgOilDoubtList> selectByList(Page<DsgOilDoubtList> page, DsgOilDoubtList en) {
        Page doubtListPage = new Page();
        List<DsgOilDoubtList> list = baseMapper.selectDsgOilDoubtList(en);
        if (CollectionUtil.isNotEmpty(list)) {
            List<DsgSysParameter> listA = dsgSysParameterService.selectByList("DSG_OIL_DOUBT_LIST_DOUBT_CODE_A");
            List<DsgSysParameter> listB = dsgSysParameterService.selectByList("DSG_OIL_DOUBT_LIST_DOUBT_CODE_B");
            List<DsgSysParameter> listC = dsgSysParameterService.selectByList("DSG_OIL_DOUBT_LIST_DOUBT_CODE_C");
            Map<String, List<DsgOilDoubtList>> map = list.stream().collect(Collectors.groupingBy(I -> I.getPayAccountNo()));
            List<Map<String,Object>> returnList = new ArrayList<>();
            for (String key : map.keySet()) {
                List<DsgOilDoubtList> dsgOilDoubtLists = map.get(key);
                DsgOilDoubtList dodl = dsgOilDoubtLists.get(0);
                Map<String, String> doubtMap = dsgOilDoubtLists.stream().collect(Collectors.toMap(DsgOilDoubtList::getDoubtCode, DsgOilDoubtList::getDoubtLevel, (oldData, newData) -> newData));
                //
                Map<String, Object> returnMap = new HashMap<>();
                returnMap.put("payAccountNo", key);
                List<DsgOilDoubtList> dsgOilDoubtListListA = new ArrayList<>();
                //存疑A
                listA.forEach(A -> {
                    DsgOilDoubtList dsgOilDoubtList = new DsgOilDoubtList();
                    dsgOilDoubtList.setColId(dodl.getColId());
                    dsgOilDoubtList.setPayAccountNo(dodl.getPayAccountNo());
                    dsgOilDoubtList.setDoubtLevel("A");
                    dsgOilDoubtList.setDoubtCode(A.getParaKey());
                    dsgOilDoubtList.setDoubtName(A.getParaValue());
                    if (doubtMap.containsKey(A.getParaKey()) && "A".equals(doubtMap.get(A.getParaKey()))) {
                        dsgOilDoubtList.setInDoubt("1");
                        dsgOilDoubtList.setCusNo(dodl.getCusNo());
                    } else {
                        dsgOilDoubtList.setInDoubt("0");
                    }
                    dsgOilDoubtListListA.add(dsgOilDoubtList);
                });
                returnMap.put("A", dsgOilDoubtListListA);
                //存疑B
                List<DsgOilDoubtList> dsgOilDoubtListListB = new ArrayList<>();
                listB.forEach(B -> {
                    DsgOilDoubtList dsgOilDoubtList = new DsgOilDoubtList();
                    dsgOilDoubtList.setColId(dodl.getColId());
                    dsgOilDoubtList.setPayAccountNo(dodl.getPayAccountNo());
                    dsgOilDoubtList.setDoubtLevel("B");
                    dsgOilDoubtList.setDoubtCode(B.getParaKey());
                    dsgOilDoubtList.setDoubtName(B.getParaValue());
                    if (doubtMap.containsKey(B.getParaKey()) && "B".equals(doubtMap.get(B.getParaKey()))) {
                        dsgOilDoubtList.setInDoubt("1");
                        dsgOilDoubtList.setCusNo(dodl.getCusNo());
                    } else {
                        dsgOilDoubtList.setInDoubt("0");
                    }
                    dsgOilDoubtListListB.add(dsgOilDoubtList);
                });
                returnMap.put("B", dsgOilDoubtListListB);
                //存疑C
                List<DsgOilDoubtList> dsgOilDoubtListListC = new ArrayList<>();
                listC.forEach(C -> {
                    DsgOilDoubtList dsgOilDoubtList = new DsgOilDoubtList();
                    dsgOilDoubtList.setColId(dodl.getColId());
                    dsgOilDoubtList.setPayAccountNo(dodl.getPayAccountNo());
                    dsgOilDoubtList.setDoubtLevel("C");
                    dsgOilDoubtList.setDoubtCode(C.getParaKey());
                    dsgOilDoubtList.setDoubtName(C.getParaValue());
                    if (doubtMap.containsKey(C.getParaKey()) && "C".equals(doubtMap.get(C.getParaKey()))) {
                        dsgOilDoubtList.setInDoubt("1");
                        dsgOilDoubtList.setCusNo(dodl.getCusNo());
                    } else {
                        dsgOilDoubtList.setInDoubt("0");
                    }
                    dsgOilDoubtListListC.add(dsgOilDoubtList);
                });
                returnMap.put("C", dsgOilDoubtListListC);
                returnList.add(returnMap);
            }
            doubtListPage.setRecords(returnList);
            doubtListPage.setTotal(returnList.size());
        }
        return doubtListPage;
    }
}
